#!/usr/bin/env python3

'''
main()
'''
from gender import *
from gender_prediction_api import *
from multiprocessing import Pool
from config import *
import pymongo

#配置mongodb的信息
client = pymongo.MongoClient(MONGO_URL)
#连接collaboration_dataset库
db_collaboration_info = client[MONGO_COLLABORATION_DB]

'''
获取到所有researcher_sex_info信息，并调用gender_prediction接口
'''
def get_all_researcher_sex_info(follow):
    researcher_sex_info = db_collaboration_info['exp_2008_to_2017_researcher_sex_info'].find({}).skip(follow*100).limit(100)

    #data = researcher_sex_info[382040:382050]
    #for data_item in data:
    #    print(data_item)

    #gender_result_list = []
    i = 0
    for data_item in researcher_sex_info:
        gender_prediction_result = use_gender_prediction_api(data_item,follow,i)

        print("-----------------------------------------------")
        print("gender_prediction_result:",gender_prediction_result)

        male = gender_prediction_result["male"]
        if male > 0.5:
            print(11111)
            db_collaboration_info['exp_2008_to_2017_researcher_sex_info'].update({"AuId":data_item["AuId"]},{"$set":{"Sex":1}})

        else:
            print(222222222222)
            #update({"AuN":researcher_name,"AfN":researcher_affiliation},{"$set":{"Sex":0}})
            db_collaboration_info['exp_2008_to_2017_researcher_sex_info'].update({"AuId":data_item["AuId"]},{"$set":{"Sex":0}})

        #gender_result_list.append(gender_prediction_result)
        i += 1
    #print(gender_result_list)
    return None


'''
调用use_gender_prediction方法
'''
def use_gender_prediction_api(researcher_sex_info,follow,i):

    gender = gender_prediction(researcher_sex_info["AuN"],researcher_sex_info["AfN"],follow,i)
    #print(researcher_sex_info["AuN"],researcher_sex_info["AfN"])
    gender_result = gender.use_gender_prediction()
    return gender_result

def main():

    #382046
    #3830
    #1130
    #(1800,1850)
    follow_list = [i for i in range(1)]
    # follow_list = [1201,1202,1214,1235,1238,1240,1241,1254,1256,1257,1258,1259,1273,1274,1289,1305]

    for name_item in follow_list:
        os.mkdir("../image/" + str(name_item))

    pool = Pool(processes = 5)
    gender_results = pool.map(get_all_researcher_sex_info,follow_list)
    print(gender_results)
    #for i in range(5):
        #get_all_researcher_sex_info(i)

    print("结束了2333333333")


if __name__ == '__main__':
    main()
